package com.mvc.controller;

import com.mvc.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

//@CrossOrigin
@Controller
public class EmployeeController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("emp")
    public String page(){
        return "emp";
    }

    @RequestMapping("/searchEmployee")
    public String findEmployee(String realname, Model model){
        List<Employee> employeeList = jdbcTemplate.query("select * from employee where realname like concat('%',?,'%') ",
                new BeanPropertyRowMapper(Employee.class),realname);
        model.addAttribute("employees",employeeList);
        return "emp";
    }

    @ResponseBody
    @RequestMapping("/searchEmployee2")
    public List<Employee> searchEmployee(String realname){
        List<Employee> employeeList = jdbcTemplate.query("select * from employee where realname like concat('%',?,'%') ",
                new BeanPropertyRowMapper(Employee.class),realname);
        return employeeList;
    }
}
